//
//  EFCollectionViewController.m
//  EnjogFood
//
//  Created by 王芳 on 17/5/1.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFCollectionView.h"
#import "EFAutoScrollTableCell.h"
#import "EFAutoScorllItem.h"
#import "EFTipItem.h"
#import "EFTipCell.h"
#import "EFSepecialItem.h"
#import "EFSpecialOfferCell.h"
#import "EFBannerItem.h"
#import "EFBannerCell.h"
#import "EFSaleListItem.h"
#import "EFFlashCell.h"
#import "EFPromotionCell.h"
#import "EFPromotionItem.h"
#import "EFFeatureCell.h"
#import "EFFearchItem.h"
#import "EFCarouseTotalItem.h"
#import "EFMessageTotalItem.h"
#import "EFAutoTotalScrollItem.h"
#import "EFSaleTotalItem.h"
#import "EFPromotionTotalItem.h"
#import "EFFeatureTotalItem.h"
#import "EFSpecialTotalItem.h"
#import "BBCyclingLabel.h"
//#import "EFFlashViewController.h"
#import "EFHomeMessageView.h"
#import "BMHomeMenuTableViewCell.h"
#import "SQHomeMenuItem.h"
#import "SQHomeMenuListItem.h"

@interface EFCollectionView ()<UITableViewDelegate,UITableViewDataSource>
// 滚动小广告
@property (nonatomic, strong) NSTimer* promptTimer;
@property (nonatomic, assign) NSUInteger promptIndex;//滚动到第几个广告
@property (nonatomic, assign) NSTimeInterval promptInterval;
@property (strong, nonatomic)  EFMessageTotalItem *tipItem;
@property (assign, nonatomic) NSInteger tipIndex;
@property (strong, nonatomic) BBCyclingLabel *tipLabel;
@property (strong, nonatomic) NSString *currentTip;
@end

@implementation EFCollectionView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.dataSourceArray = [[NSMutableArray alloc] init];
        [self addSubview:self.collectionTableView];
        self.promptInterval = 3;
    }
    return self;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   // for (NSInteger index = 0; index < self.dataSourceArray.count; index++) {
        NSObject *item = [self.dataSourceArray objectAtIndex:indexPath.row];
        if ([item isKindOfClass:[EFCarouseTotalItem class]]) {
            return 200;
        }
        if ([item isKindOfClass:[EFMessageTotalItem class]]) {
            return 30;
        }
        if ([item isKindOfClass:[EFSpecialTotalItem class]]) {
            return 211;
        }
        if ([item isKindOfClass:[EFBannerItem class]]) {
            return 130;
        }
        if ([item isKindOfClass:[EFSaleTotalItem class]]) {
            return 285;
        }
        if ([item isKindOfClass:[EFPromotionItem class]]) {
            return 300;
        }
        if ([item isKindOfClass:[EFFearchItem class]]) {
            //单独计算
            return  [EFFeatureCell cellHeight:(EFFearchItem *)item];
        }
        return 200;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     NSString *identifierStr = @"";
      NSObject *item = [self.dataSourceArray objectAtIndex:indexPath.row];
    if ([item isKindOfClass:[EFMessageTotalItem class]]) {
        identifierStr = @"tipCell";
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierStr];
    
        if ([item isKindOfClass:[EFCarouseTotalItem class]]) {
            identifierStr = @"autoScrollCell";
            if (cell == NULL) {
                cell = [[EFAutoScrollTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierStr];
            }
            [(EFAutoScrollTableCell *)cell setObject:[(EFCarouseTotalItem *)item list]];
        }else
        if ([item isKindOfClass:[EFMessageTotalItem class]]) {
            identifierStr = @"tipCell";
            if (cell == NULL) {
                cell = [[EFTipCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierStr];
            }
 
            [(EFTipCell *)cell setObject:(EFMessageTotalItem *)item index:0];
           self.tipLabel = ((EFTipCell *)cell).tipLabel;
            self.currentTip = ((EFTipCell *)cell).currentMessage;
            //为tipCell增加删除功能
            __weak typeof(self) weakSelf = self;
            ((EFTipCell *)cell).deleteBlock = ^(){
                //去掉datasource里的tip内容
                [weakSelf.dataSourceArray removeObject:item];
                [weakSelf.collectionTableView reloadData];
            };
            ((EFTipCell *)cell).showBlock = ^(NSString *content){
                EFHomeMessageView *messageV = [[EFHomeMessageView alloc] initWithFrame:CGRectMake(0, 0, mScreenWidth, mScreenWidth) withContent:content];
                UIWindow *window = [UIApplication sharedApplication].keyWindow;
                [window addSubview:messageV];
            };
            }else
        if ([item isKindOfClass:[EFSpecialTotalItem class]]) {
            identifierStr = @"specialOfferCell";
            if (cell == NULL) {
                cell = [[EFSpecialOfferCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierStr];
            }
            [(EFSpecialOfferCell *)cell setObject:[(EFSpecialTotalItem *)item list]];

        }else
        if ([item isKindOfClass:[EFBannerItem class]]) {
            identifierStr = @"banner";
            if (cell == NULL) {
                cell = [[EFBannerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierStr];
            }
            [(EFBannerCell *)cell setObject:(EFBannerItem *)item];

        }else
        if ([item isKindOfClass:[EFSaleTotalItem class]]) {
            identifierStr = @"timeSaleCell";
            if (cell == NULL) {
                cell = [[EFFlashCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierStr];
            }
            [(EFFlashCell *)cell setObject:[(EFSaleTotalItem *)item list]];
            ((EFFlashCell *)cell).click = ^(){
                //根据id跳转
//                EFFlashViewController *vc = [[EFFlashViewController alloc] initWithEventId:((EFSaleTotalItem *)item).eventId];
//                [ICNavigator pushViewController:vc animated:YES];
            };
        }else
        if ([item isKindOfClass:[EFPromotionItem class]]) {
           identifierStr = @"promotionCell";
            if (cell == NULL) {
                cell = [[EFPromotionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierStr];
            }
            [(EFPromotionCell *)cell setObject:(EFPromotionItem *)item];

        }else
        if ([item isKindOfClass:[EFFearchItem class]]) {
            identifierStr = @"featureCell";
            if (cell == NULL) {
                cell = [[EFFeatureCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierStr];
            }
            [(EFFeatureCell *)cell setObject:(EFFearchItem *)item];
        }
 
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (void)handlePromptTimer {
    if (self.tipItem != nil) {
        if (self.promptIndex >= self.tipItem.list.count) {
            self.promptIndex = 0;
        }
        if (self.tipItem.list.count > 0 && self.tipIndex < self.tipItem.list.count && self.promptIndex <self.tipItem.list.count) {
            EFTipItem *tipItem = (EFTipItem *)[self.tipItem.list objectAtIndex:self.promptIndex];
            [self.tipLabel setText:tipItem.content animated:NO];
            UITableViewCell *tempCell = [self.collectionTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
            if ([tempCell isKindOfClass:[EFTipCell class]]) {
                EFTipCell *cell = (EFTipCell *)tempCell;
                cell.currentMessage = tipItem.content;
                self.promptIndex ++;
            }
           
       }
    }
}  

- (UITableView *)collectionTableView{
    if (!_collectionTableView) {
        _collectionTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64 - 50 - 40) style:UITableViewStylePlain];
        _collectionTableView.dataSource = self;
        _collectionTableView.delegate = self;
        _collectionTableView.backgroundColor = [UIColor whiteColor];
        _collectionTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _collectionTableView;
}

- (NSTimer *)promptTimer {
    if (!_promptTimer) {
        _promptTimer = [NSTimer scheduledTimerWithTimeInterval:self.promptInterval target:self selector:@selector(handlePromptTimer) userInfo:nil repeats:YES];
    }
    return _promptTimer;
}

- (void)setPromptInterval:(NSTimeInterval)promptInterval {
    if (promptInterval > 0) {
        _promptInterval = promptInterval;
        [self.promptTimer invalidate];
        self.promptTimer = nil;
        if (self.tipItem == nil || self.tipItem.list.count < 1) {
            return;
        }
        [self.promptTimer fire];
    }
}

- (void)setDataSourceArray:(NSMutableArray *)dataSourceArray{
    _dataSourceArray = dataSourceArray;
    for (NSInteger index = 0; index < self.dataSourceArray.count; index++) {
        NSObject *item = [self.dataSourceArray objectAtIndex:index];
        if ([item isKindOfClass:[EFMessageTotalItem class]]) {
            self.tipIndex = index;
            self.tipItem = (EFMessageTotalItem *)item;
        }
    }
    //如果有tip 并且大于1条 启动timer
    if (self.tipItem != nil || self.tipItem.list.count > 1) {
        [self.promptTimer invalidate];
        self.promptTimer = nil;
        [self.promptTimer fire];
    }
}

- (void)stopTimer{
    if (self.promptTimer != nil) {
        [self.promptTimer invalidate];
        self.promptTimer = nil;
    }
}

- (void)stopScrollView{
    //获取item
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    UITableViewCell *cell = [self.collectionTableView cellForRowAtIndexPath:indexPath];
    [(EFAutoScrollTableCell *)cell scrollStop];
}

- (void)dealloc{
    [self stopTimer];
}


@end
